/**
 * 
 */
package main;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;

/**
 * @author WenHao
 *
 */
public class Main extends Applet {
	private static Wallet wallet = null;

	public Main(){
		this.wallet = new Wallet();
	}

	public static void install(byte[] bArray, short bOffset, byte bLength) {
		// GP-compliant JavaCard applet registration
		new Main().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
	}

	public void process(APDU apdu) {
		//Good practice: Return 9000 on SELECT
		if (selectingApplet()) {
			return;
		}
		apdu.setIncomingAndReceive();

		byte[] buf = apdu.getBuffer();
		switch (buf[ISO7816.OFFSET_INS]) {
			case (byte) Wallet.DEPOSIT_INS:
				this.wallet.deposit(apdu);
				break;
			case (byte) Wallet.WITHDRAWL_INS:
				this.wallet.withdrawl(apdu);
				break;
			case (byte) Wallet.CHECK_MONEY_INS:
				this.wallet.checkBalance(apdu);
				break;

			default:
				// good practice: If you don't know the INStruction, say so:
				ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
		}
	}
}
